import Lesson from './Lesson.js'
import Book from './Book.js'
export default class Study {
  constructor() {
    this.author = 'czx'
    this.lessons = []
    this.init()
  }
  init() {
    let json = {
      lessons: [
        {
          name: '语文',
          books: [
            {
              name: '课文',
              page: 200
            },
            {
              name: '亮点',
              page: 200
            }
          ]
        },
        {
          name: '数学',
          books: [
            {
              name: '课文',
              page: 200
            },
            {
              name: '实验班',
              page: 200
            }
          ]
        },
        {
          name: '英语',
          books: [
            {
              name: '课文',
              page: 200
            },
            {
              name: '亮点',
              page: 200
            }
          ]
        }
      ]
    }

    json.lessons.forEach((res) => {
      let lesson = new Lesson({
        name: res.name
      })
      res.books.forEach((res) => {
        lesson.addBook(
          new Book({
            name: res.name,
            page: res.page
          })
        )
      })
      this.lessons.push(lesson)
    })
  }
  toJSON() {
    let json = {
      author: this.author,
      lessons: this.lessons.map((res) => res.toJSON())
    }
    return json
  }
  fromJSON(json) {
    this.author = json.author
    this.lessons.length = 0
    json.lessons.forEach((res) => {
      this.lessons.push(new Lesson().fromJSON(res))
    })
    return this
  }
}
